package com.cumt.threeway.view.dialog;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.cumt.threeway.R;
import com.cumt.threeway.activity.PersonInfoActivity;
import com.cumt.threeway.util.ThreeWayUtil;

/**
 * Created by 胡晶晶 on 2016/5/25.
 */
public class EditNickNameDialog extends DialogFragment implements View.OnClickListener{

    private Context mContext;
    private EditText mEditTextNickName;
    private Button mBtnDel;
    private String mNickName;

    private Handler mHandler = new Handler();

    public static EditNickNameDialog newInstance() {
        return new EditNickNameDialog();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int styleNormal = android.support.v4.app.DialogFragment.STYLE_NORMAL;
        int theme = android.R.style.Theme_Light_NoTitleBar_Fullscreen;
        setStyle(styleNormal, theme);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_edit_nick_name_layout, null, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mNickName = ThreeWayUtil.getInstance().getUserName(mContext);
        mEditTextNickName = (EditText) view.findViewById(R.id.et_nick_name);
        mBtnDel = (Button) view.findViewById(R.id.btn_del1);
        mBtnDel.setOnClickListener(this);
        view.findViewById(R.id.iv_back).setOnClickListener(this);
        view.findViewById(R.id.tv_confirm).setOnClickListener(this);
        if(!TextUtils.isEmpty(mNickName)) {
            mEditTextNickName.setText(mNickName);
            mBtnDel.setVisibility(View.VISIBLE);
        }

        mEditTextNickName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s)) {
                    mBtnDel.setVisibility(View.VISIBLE);
                } else {
                    mBtnDel.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        if(v == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.btn_back:
                dismiss();
                break;
            case R.id.tv_confirm:
                if(TextUtils.isEmpty(getEditContent(mEditTextNickName))) {
                    ThreeWayUtil.getInstance().showToast(mContext,getResources().getString(R.string.edit_nick_name_error));
                } else {
                    ThreeWayUtil.getInstance().showToast(mContext, getResources().getString(R.string.edit_success));
                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            dismiss();
                            ((PersonInfoActivity) mContext).setUserNickName(getEditContent(mEditTextNickName));
                            ThreeWayUtil.getInstance().saveUserName(mContext, getEditContent(mEditTextNickName));
                        }
                    }, 600);
                }
                break;
            case R.id.btn_del1:
                mEditTextNickName.setText("");
                break;
        }
    }

    private String getEditContent(EditText editText) {
        return editText.getText().toString();
    }
}
